package org.lwd.datalink.domain.task;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.Alias;
import org.lwd.datalink.domain.BaseEntity;
import org.lwd.datalink.domain.Storable;
import org.lwd.datalink.errors.DatalinkException;

import java.io.Serializable;
import java.util.Date;

@Alias("taskShadow")
@Getter
@Setter
public class TaskShadowInfo extends BaseEntity implements Serializable, Storable {
    private Long id;
    private Long taskId;
    private State state;
    private String parameter;
    private String note;

    public void setParameter(String parameter) {
        this.parameter = parameter;
        if (!StringUtils.isEmpty(parameter)) {
            parameterObj = JSONObject.parseObject(parameter, new TypeReference<TaskShadowParameter>() {}, Feature.SupportAutoType);
        } else {
            parameterObj = null;
        }
    }


    private transient TaskShadowParameter parameterObj;

    public TaskShadowParameter getParameterObj() {
        if (parameterObj == null) {
            throw new DatalinkException("Task-shadow-parameter can not be null.");
        }
        return parameterObj;
    }

    public static enum State {
        /**
         * 初始化
         */
        INIT,
        /**
         * 执行中
         */
        EXECUTING,
        /**
         * 已完成
         */
        COMPLETE,
        /**
         * 已废弃
         */
        DISCARD
    }

    //------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------fields for business------------------------------------------------------
    //------------------------------------------------------------------------------------------------------------------
    private TaskInfo taskInfo;
}
